package com.itp.uploadfile;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

	private final String TAG = "internet";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String strUrl = "http://www.web.com/upload/upload.php"; 
		String filename = "test.zip";   

		try {
			String result = runHttpPost(strUrl, filename);
			
			Log.d(TAG, "response " + result);
			Toast.makeText(this, result, 0).show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	
	private String runHttpPost(String strUrl, String filename) throws IOException {
		
		String Title = "test";
		String Description = "desc test";
		
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "***5870145CT**";
        
	    File output = new File(Environment.getExternalStorageDirectory(),filename);
	    FileInputStream fileInputStream = new FileInputStream(output);        
        
		URL url = new URL(strUrl);
		try {			
			HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setDoInput(true);
            httpCon.setDoOutput(true);
            httpCon.setUseCaches(false);			
			httpCon.setRequestMethod("POST");
			httpCon.setRequestProperty("Connection", "Keep-Alive");
            httpCon.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            DataOutputStream dos = new DataOutputStream(httpCon.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes(Title);
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes(Description);
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + filename +"\"" + lineEnd);
            dos.writeBytes(lineEnd);
            int bytesAvailable = fileInputStream.available();
            int maxBufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[ ] buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0,bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            fileInputStream.close();
                
            dos.flush();
                
            Log.e(TAG,"File uploaded, Response: "+String.valueOf(httpCon.getResponseCode()));
                 
            InputStream is = httpCon.getInputStream();
            int ch;

            StringBuffer b =new StringBuffer();
            while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); }
            String sRes = b.toString();
            Log.d(TAG, "response " + sRes);
            dos.close();
            return sRes ;
                        

		} catch (Exception ex) {
			Log.d(TAG, "Problem reading " + ex.getLocalizedMessage());
			return null;
		} 
	}			

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
